<?php
class ZpKpsController extends AppController {

	var $name = 'ZpKps';
	var $helpers = array('Html','Javascript', 'Ajax');
	var $components=array('Cookie');
	var $uses = array('ZpKp','Ctrinh','Hangmuc','Tien');

	function index() {
		
		$kqs=array();$tenct=array();
		if(!empty($_POST['ngaybd'])){
			$ngaybd=$_POST['ngaybd'];
			$ngaykt=$_POST['ngaykt'];
			$this->Cookie->write('ngaybd',$ngaybd);
			$this->Cookie->write('ngaykt',$ngaykt);
			
			
			//chua ket qua tien cua cac noi dung (lon)
			$kqs=$this->ZpKp->find('all',array(
				'conditions'=>array('ngaybd between ? and ?'=>array($ngaybd,$ngaykt)),
				'order'=>array('ctrinh_id'=>'asc','noidung_id'=>'asc')));
			
			$idct=$this->ZpKp->find('list',array(
				'conditions'=>array('ngaybd between ? and ?'=>array($ngaybd,$ngaykt)),
				'fields'=>array('ctrinh_id','ctrinh_id')));


			$this->Ctrinh->recursive=0;
			$tenct=$this->Ctrinh->find('all',array(
				'conditions'=>array('Ctrinh.id'=>$idct),
				'fields'=>array('Ctrinh.id','tenctrinh','Dvthicong.tendvthicong'),
				'order'=>array('Ctrinh.id'=>'asc')));
		
			$this->Hangmuc->recursive=-1;
			$sohm=$this->Hangmuc->find('all',array(
				'conditions'=>array('ctrinh_id'=>$idct),
				'fields'=>array('ctrinh_id','count(id) as sohm'),
				'group'=>array('ctrinh_id'),
				'order'=>array('ctrinh_id'=>'asc')));
			
			$this->Tien->recursive=0;
			$tienloaivon=$this->Tien->find('all',array(
				'conditions'=>array('ctrinh_id'=>$idct),
				'fields'=>array('ctrinh_id','Nguonvon.loaivon_id','sum(sotienvon) as tienvon'),
				'group'=>array('ctrinh_id','Nguonvon.loaivon_id'),
				'order'=>array('ctrinh_id'=>'asc','loaivon_id'=>'asc')));
		
		}

		$this->set(compact('idct','kqs','tenct','sohm','tienloaivon','ngaykt','ngaybd'));

	}
	function xls() {
		$this->layout='';
		
		$ngaybd=$this->Cookie->read('ngaybd');
		$ngaykt=$this->Cookie->read('ngaykt');
			
	
			
			//chua ket qua tien cua cac noi dung (lon)
			$kqs=$this->ZpKp->find('all',array(
				'conditions'=>array('ngaybd between ? and ?'=>array($ngaybd,$ngaykt)),
				'order'=>array('ctrinh_id'=>'asc','noidung_id'=>'asc')));
			
			$idct=$this->ZpKp->find('list',array(
				'conditions'=>array('ngaybd between ? and ?'=>array($ngaybd,$ngaykt)),
				'fields'=>array('ctrinh_id','ctrinh_id')));


			$this->Ctrinh->recursive=0;
			$tenct=$this->Ctrinh->find('all',array(
				'conditions'=>array('Ctrinh.id'=>$idct),
				'fields'=>array('Ctrinh.id','tenctrinh','Dvthicong.tendvthicong'),
				'order'=>array('Ctrinh.id'=>'asc')));
		
			$this->Hangmuc->recursive=-1;
			$sohm=$this->Hangmuc->find('all',array(
				'conditions'=>array('ctrinh_id'=>$idct),
				'fields'=>array('ctrinh_id','count(id) as sohm'),
				'group'=>array('ctrinh_id'),
				'order'=>array('ctrinh_id'=>'asc')));
			
			$this->Tien->recursive=0;
			$tienloaivon=$this->Tien->find('all',array(
				'conditions'=>array('ctrinh_id'=>$idct),
				'fields'=>array('ctrinh_id','Nguonvon.loaivon_id','sum(sotienvon) as tienvon'),
				'group'=>array('ctrinh_id','Nguonvon.loaivon_id'),
				'order'=>array('ctrinh_id'=>'asc','loaivon_id'=>'asc')));
		
	
		$this->set(compact('idct','kqs','tenct','sohm','tienloaivon','ngaykt','ngaybd'));

	}

	
}
